<!-- eslint-disable vue/no-unused-vars -->
<template>
  <div class="zw-file-gallary">
    <div class="file-item" v-for="item in fileList" :key="item.guid">
      <div class="icon">
        <office-icon style="font-size: 40px;" :type="item.fileType.replace('.', '')"></office-icon>
      </div>
      <div class="meta">
        <el-text style="width: 100%;" class="name" truncated :title="item.fileName">
          {{ item.fileName }}
        </el-text>
        <div class="size">
          <span>{{ (item.fileSize / 1024 / 1024).toFixed(2) + "MB" }}</span>
          <div style="margin-left: 24px; display: flex;">
            <el-button size="small" type="primary" link @click="download(item.guid)">下载</el-button>
            <el-button size="small" v-if="!readonly" link type="danger" @click="onRemove(item)"
              style="margin-left: 8px;">删除</el-button>
          </div>
        </div>

      </div>
    </div>
    <div v-if="!readonly" class="upload-file" title="点击上传文件">
      <el-upload multiple name="files" :limit="5" :action="uploadUri" :show-file-list="false"
        :on-success="uploadSuccess">
        <el-icon>
          <CirclePlus />
        </el-icon>
      </el-upload>
    </div>
    <div v-if="readonly && fileList.length === 0" class="no-files">
      暂无数据
    </div>
  </div>
</template>
<script lang="ts" setup>
import OfficeIcon from '../Icon/OfficeIcon.vue';
import ApiTree from '@/server/api-tree';
import type { FileInfoDTO } from '@/server/file/types';
import type { RestResult } from '@/server/server-response';
import { ElMessage } from 'element-plus';

defineProps<{
  readonly: boolean,
  fileList: FileInfoDTO[]
}>();
const emits = defineEmits(["upload", "remove"])
const uploadUri = ApiTree.file.getUploadUri();
const download = (fileId: string) => {
  ApiTree.file.browserDownload(fileId);
}

const uploadSuccess = (response: RestResult<string[]>) => {
  if (response.code === 200) {
    ElMessage.success(response.msg);
    emits("upload", response.data);
  } else {
    ElMessage.error(response.msg);
  }
}
const onRemove = (file: FileInfoDTO) => {
  emits("remove", file.guid);
}
</script>
<style lang="scss" scope>
.zw-file-gallary {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;

  .file-item {
    height: 56px;
    width: 260px;
    display: flex;
    flex-direction: row;
    padding: 8px;
    align-items: center;
    border: 1px solid v.$border-color;
    border-radius: 5px;
    margin-right: 8px;
    margin-bottom: 8px;

    .meta {
      width: 200px;
      display: flex;
      flex-direction: column;
      padding-left: 4px;

      .name {
        font-size: 12px;
        font-weight: 600;
        height: 24px;
        line-height: 24px;
      }

      .size {
        font-size: 10px;
        font-weight: 600;
        display: flex;
      }
    }

    .icon {
      width: 40px;
      height: 40px;
      line-height: 40px;
    }
  }

  .upload-file {
    border-radius: 5px;
    height: 56px;
    width: 56px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid v.$border-color;
    font-size: 24px;
    font-weight: 600;
  }

  .no-files {
    border-radius: 5px;
    width: 100px;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid v.$border-color;
  }
}
</style>
